#include	"a_global_vars.h"
int setsock_nonblock(int fd) {
	int flags;

	flags = fcntl(fd, F_GETFL);
	if (flags < 0)
		return flags;

	if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
		return -1;

	return 0;
}
//------------------------------------------------------------------------------
int create_tcp_client_socket(int port, const char *ip) {
	struct sockaddr_in server_addr;
	int e = -1;
	int sid;
	//	memset(&server_addr, '\0', sizeof(struct sockaddr_in));

	sid = socket(AF_INET, SOCK_STREAM, 0);

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(ip);
	server_addr.sin_port = htons(port);

	while (e < 0) {
		e = connect(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
		if (e >= 0)
			break;
		close(sid);
		sleep(1);
		sid = socket(AF_INET, SOCK_STREAM, 0);
	}
	return sid;
}
//------------------------------------------------------------------------------
int create_tcp_server_socket(int port, const char *ip) {

	struct sockaddr_in server_addr, client_addr;
	int sid, e, activate = 1, new_sid;
	unsigned int client_addr_len;

	client_addr_len = sizeof(client_addr);
	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = INADDR_ANY;
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(struct sockaddr));
	assert(e >= 0);
	e = listen(sid, 1);

	printf("Server:  Waiting for connection requests from learners...\n");
	new_sid = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
	printf("connection request came from the node with port number: %d\n", ntohs(client_addr.sin_port));

	return new_sid;
}
//------------------------------------------------------------------------------
void create_tcp_server_socket_for_star(char * ip, int port, int expected_num_connections) {
	unsigned int client_addr_len;
	int sid, e = -1, c_counter = 0, activate = 1;
	struct sockaddr_in server_addr, client_addr;

	client_addr_len = sizeof(client_addr);

	sid = socket(AF_INET, SOCK_STREAM, 0);
	setsockopt(sid, SOL_SOCKET, SO_REUSEADDR, &activate, sizeof(int));

	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = INADDR_ANY;//, inet_addr(ip);
	server_addr.sin_port = htons(port);

	e = bind(sid, (struct sockaddr*) &server_addr, sizeof(server_addr));
	assert(e >= 0);

	e = listen(sid, 1);
	assert(e >= 0);

	printf("Acceptor waiting for connection requests from processes on the ring...%d\n", expected_num_connections);
	int ssid;
	int i;
	for (i = 0; i < get_config_info()->num_learners_to_connect_to_one_acceptor; i++) {
		ssid = accept(sid, (struct sockaddr*) &client_addr, &client_addr_len);
		char str[INET_ADDRSTRLEN];
		inet_ntop(AF_INET, &(client_addr.sin_addr.s_addr), str, INET_ADDRSTRLEN);
		int learner_id = find_learner_given_ip(get_config_info(), str);
		get_config_info()->learner_infos[learner_id].learner_to_acceptor_sid = ssid;
		assert(strcmp(get_config_info()->learner_infos[learner_id].ip, str) == 0);
		printf("connection request came from the node with port number:  %d	socket: %d	ip: %s.\n", ntohs(client_addr.sin_port), ssid, str);
	}

}

